package net.ewon.avatar.pkgmgmt;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageFile;
import net.ewon.avatar.common.pkg.AvatarPackageWithFile;
import net.ewon.avatar.common.pkg.Version;
import net.ewon.avatar.pkgmgmt.manager.AvatarPackageManager;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

@Consumes("application/json")
@Produces("application/json")
public class AvatarPkgMgmtService {

	private static final Logger logger = Logger
			.getLogger(AvatarPkgMgmtService.class);

	@Autowired
	private AvatarPackageManager avatarPackageManager;

	@POST
	@Path("/uploadAvatarPackageWithFiles")
	public String uploadAvatarPackageFiles(AvatarPackageWithFile pair)
			throws Exception {
		AvatarPackage avatarPackage = pair.getAvatarPackage();
		List<AvatarPackageFile> avatarPackageFiles = pair
				.getAvatarPackageFiles();
		logger.debug("The number of upload files:" + avatarPackageFiles.size());

		try {
			removeAvatarPackage(avatarPackage);

			for (AvatarPackageFile file : avatarPackageFiles) {
				avatarPackageManager.storeAvatarPackageFile(file);
				avatarPackage.addFileId(file.getId());
			}

			avatarPackageManager.store(avatarPackage);
			logger.info("Succeed to upload avatar package name="
					+ avatarPackage.getName() + ", version="
					+ avatarPackage.getVersion());
		} catch (Throwable e) {
			logger.warn(
					"Failed to upload avatar package with files, try to roll back",
					e);
			for (AvatarPackageFile file : avatarPackageFiles) {
				if (file.getId() != null) {
					avatarPackageManager.removePackageFile(file.getId());
				}
			}
		}

		return "OK";
	}

	@POST
	@Path("/removeAvatarPackage")
	public void removeAvatarPackage(AvatarPackage avatarPackage) {

		AvatarPackage existingAvatarPackage = avatarPackageManager
				.getAvatarPackage(avatarPackage.getName(),
						avatarPackage.getVersion());

		if (existingAvatarPackage != null) { 
			List<String> fileIds = existingAvatarPackage.getFileIds();
			avatarPackageManager.removePackage(existingAvatarPackage.getId());
			for (String fileId : fileIds) {
				avatarPackageManager.removePackageFile(fileId);
			}
			logger.debug("The avatar package name="
					+ avatarPackage.getName() + ",version="
					+ avatarPackage.getVersion() + " is removed.");
		} else {
			logger.debug("The avatar package name="
					+ avatarPackage.getName() + ",version="
					+ avatarPackage.getVersion() + " doesn't exists.");
		}

	}

	@GET
	@Path("/exists")
	public boolean exists(@QueryParam("pkgName") String pkgName,
			@QueryParam("version") String version) {
		return getAvatarPackage(pkgName, version) != null;
	}

	@GET
	@Path("/getAvatarPackage")
	public AvatarPackage getAvatarPackage(
			@QueryParam("pkgName") String pkgName,
			@QueryParam("version") String version) {

		AvatarPackage avatarPackage = null;

		try {
			avatarPackage = avatarPackageManager.getAvatarPackage(pkgName,
					Version.parse(version));
			logger.info("get avatarPackage=" + avatarPackage);
		} catch (Throwable e) {
			logger.error("Failed to get Avatar Package for pkgName=" + pkgName
					+ ", verison=" + version);
		}
		return avatarPackage;

	}

	@GET
	@Path("/getAvatarPackageFile")
	public AvatarPackageFile getAvatarPackageFile(
			@QueryParam("fileId") String fileId) {

		AvatarPackageFile file = null;
		try {
			file = avatarPackageManager.getAvatarPackageFile(fileId); 
		} catch (Throwable e) {
			logger.error("Failed to get Avatar Package File for fileId="
					+ fileId);
		}
		return file;

	}

}
